<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-tags"
          size="mini"
          @click="handleCycle"
        >标签管理
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="memberTaskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="#" align="center" prop="id" />
      <el-table-column label="任务标题" align="center" prop="name" />
<!--      <el-table-column label="赠送成长值" align="center" prop="growth" />-->
<!--      <el-table-column label="赠送积分" align="center" prop="intergration" />-->
      <el-table-column label="赠送小红花" align="center" prop="energy" />
      <el-table-column label="任务可完成次数" align="center" prop="num" />
      <el-table-column label="任务需达成次数" align="center" prop="finishNum" />
      <el-table-column label="任务标识" align="center" prop="taskTag" />
      <el-table-column label="任务周期" align="center" prop="taskCycle">
        <template slot-scope="scope">
          <span v-if="scope.row.taskCycle === 'y'">年</span>
          <span v-if="scope.row.taskCycle === 'm'">月</span>
          <span v-if="scope.row.taskCycle === 'w'">周</span>
          <span v-if="scope.row.taskCycle === 'd'">日</span>
          <span v-if="scope.row.taskCycle === '#'">不限</span>
        </template>
      </el-table-column>
      <el-table-column label="任务类型：0->新手任务；1->日常任务 2 -> 会员专享任务" align="center" prop="type">
        <template slot-scope="scope">
          <span v-if="scope.row.type === 0">新手任务</span>
          <span v-if="scope.row.type === 1">日常任务</span>
          <span v-if="scope.row.type === 2">活动任务</span>
          <span v-if="scope.row.type === 3">会员专享任务</span>
        </template>
      </el-table-column>
      <el-table-column label="任务状态 0->下线 1->上线" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="任务路径" align="center" prop="path" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="pagination.total > 0"
      :total="pagination.total"
      :page.sync="pagination.pageNum"
      :limit.sync="pagination.pageSize"
      @pagination="handleQuery"
    />
    <!-- 添加或修改会员任务对话框 -->
    <el-dialog :title="dialog.title" :visible.sync="dialog.visible" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="任务标题" prop="name">
          <el-input v-model="form.name" placeholder="请输入任务标题" />
        </el-form-item>
        <el-form-item label="任务图标" prop="icon">
          <single-upload v-model="form.icon" />
        </el-form-item>
        <el-form-item label="任务标识" prop="taskTag">
          <el-select
            v-model="form.taskTag"
            filterable
            reserve-keyword
            placeholder="请输入关键词"
            :loading="loading"
          >
            <el-option
              v-for="item in taskTagList"
              :key="item.id"
              :label="item.remark"
              :value="item.tag"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="任务类型">
          <el-select v-model="form.type" placeholder="请选择任务类型">
            <el-option
              v-for="item in taskTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="任务可完成次数" prop="num">
          <el-input-number v-model="form.num" :min="1" label="任务可完成次数" />
        </el-form-item>
        <el-form-item label="任务需达成次数" prop="finishNum">
          <el-input-number v-model="form.finishNum" :min="1" label="任务需达成次数" />
        </el-form-item>
<!--        <el-form-item label="赠送成长值" prop="growth">-->
<!--          <el-input-number v-model="form.growth" :min="0" label="成长值" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="赠送积分" prop="intergration">-->
<!--          <el-input-number v-model="form.intergration" :min="0" label="积分" />-->
<!--        </el-form-item>-->
        <el-form-item label="赠送小红花" prop="energy">
          <el-input-number v-model="form.energy" :min="0" label="小红花" />
        </el-form-item>
        <el-form-item label="任务路径" prop="path">
          <el-input v-model="form.path" placeholder="请输入任务跳转路径" />
        </el-form-item>
        <el-form-item v-if="form.type !== 0 && form.type !== 2" label="任务周期" prop="tag">
          <el-radio-group v-model="form.taskCycle">
            <el-radio label="y">年</el-radio>
            <el-radio label="m">月</el-radio>
            <el-radio label="w">周</el-radio>
            <el-radio label="d">日</el-radio>
            <el-radio label="#">不限</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="启用状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">正常</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number v-model="form.sort" :min="0" :step="1" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addMemberTask, delMemberTask, getMemberTask, listMemberTask, updateMemberTask } from '@/api/ums/member_task'
import SingleUpload from '@/components/Upload/SingleUpload'
import { listMember_task_tag } from '@/api/ums/member_task_tag'

const taskTypes = [
  {
    value: 0,
    label: '新手任务'
  },
  {
    value: 1,
    label: '日常任务'
  },
  {
    value: 2,
    label: '活动任务'
  },
  {
    value: 3,
    label: '会员专享任务'
  }
]
export default {
  name: 'MemberTask',
  components: { SingleUpload },
  data() {
    return {
      taskTypes: taskTypes,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      pagination: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      // 会员任务表格数据
      memberTaskList: [],
      // 弹出层
      dialog: {
        title: undefined,
        visible: false,
        type: undefined // type 操作类型：1-新增 2-修改
      },
      // 查询参数
      queryParams: {},
      // 表单参数
      form: {},
      taskTagList: null,
      // 表单校验
      rules: {
        name: [
          { required: true, message: '任务标题不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getTaskTagCycleList()
  },
  methods: {
    handleCycle() {
      this.$router.push({
        path: '/member/task/cycle'
      })
    },
    /** 查询会员任务列表 */
    getList() {
      this.loading = true
      this.queryParams.pageNum = this.pagination.pageNum
      this.queryParams.pageSize = this.pagination.pageSize
      listMemberTask(this.queryParams).then(response => {
        const { data } = response
        this.memberTaskList = data.list
        this.pagination.total = data.totalCount
        this.loading = false
      })
    },
    getTaskTagCycleList() {
      listMember_task_tag({ queryMode: 'list' }).then(response => {
        const { data } = response
        this.taskTagList = data
      })
    },
    // 取消按钮
    cancel() {
      this.reset()
      this.closeDialog()
    },
    handleReset() {
      this.pagination = {
        pageNum: 1,
        pageSize: 10,
        total: 0
      }
      this.handleQuery()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        icon: null,
        name: null,
        growth: 0,
        intergration: 0,
        energy: 0,
        path: null,
        type: null,
        status: 0,
        num: 1,
        finishNum: 1,
        taskTag: null,
        taskCycle: '#',
        remark: null,
        sort: 0
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    resetForm(val) {
      if (this.$refs[val]) {
        this.$refs[val].resetFields()
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.dialog = {
        title: '添加会员任务',
        visible: true,
        type: 'add'
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.dialog = {
        title: '修改会员任务',
        visible: true,
        type: 'edit'
      }
      const id = row.id || this.ids
      getMemberTask(id).then(response => {
        this.form = response.data
      })
    },
    // 状态修改
    handleStatusChange(row) {
      const text = row.status === 1 ? '启用' : '停用'
      this.$confirm('确认要"' + text + '【' + row.name + '"】吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return updateMemberTask(row)
        })
        .then(() => {
          this.$message.success(text + '成功')
        })
        .finally(() => {
          this.handleQuery()
        })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.dialog.type === 'edit') {
            if (this.form.type === 0 || this.form.type === 2) {
              this.form.taskCycle = '#'
            }
            updateMemberTask(this.form).then(response => {
              this.$message.success('修改成功')
              this.closeDialog()
              this.getList()
            })
          } else {
            addMemberTask(this.form).then(response => {
              this.$message.success('新增成功')
              this.closeDialog()
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id ? [row.id] : this.ids
      this.$confirm('是否确认删除会员任务编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delMemberTask(ids)
      }).then(() => {
        this.getList()
        this.$message.success('删除成功')
      })
    },
    closeDialog() {
      this.dialog = {
        title: undefined,
        visible: false,
        type: undefined
      }
    }
  }
}
</script>
